//
//  PersonalHomePageTableViewController.m
//  NewChat
//
//  Created by lanou3g on 16/3/1.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "PersonalHomePageTableViewController.h"
#import "CommunityTableViewCell.h"
#import "HeadView.h"
#import "TopicTableViewCell.h"
#import "LReplyTableViewCell.h"
#import "CustomTwoBtnvView.h"
#import "ReplyViewController.h"


@interface PersonalHomePageTableViewController ()

@property(nonatomic,strong)HeadView*headV;
@property(nonatomic,strong)CustomTwoBtnvView*bottomButton;
@end

@implementation PersonalHomePageTableViewController

static BOOL flag = YES;

-(instancetype)initWithStyle:(UITableViewStyle)style{
    
    if (self = [super initWithStyle:UITableViewStylePlain]) {
        
    }
    return self;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
     NSLog(@"这个页面是 社区-->PersonalHomePageTableViewController");

    self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    
    //设置导航栏透明
    self.navigationController.navigationBar.alpha = 1;
    //消失时移除底部的bottomButton
    [self.bottomButton removeFromSuperview];
    self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"这个页面是 社区-->PersonalHomePageTableViewController");
    self.view.backgroundColor = [UIColor greenColor];
    self.navigationController.navigationBarHidden = NO;//导航控制器不隐藏
    self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去除黑线
    self.tabBarController.tabBar.hidden = YES; //隐藏底部tabbar
    //设置头视图
    [self setTableHeaderView];
    //添加底部按钮
    [self addButtonAtBotttom];
    
    //注册两个cell
    UINib *replyNib = [UINib nibWithNibName:@"LReplyTableViewCell" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:replyNib forCellReuseIdentifier:@"LReplyCell"];
    [self.tableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"TopCell"];
    
    //设置rightBarButtonItem
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(clickRightBarButtonItemAction:)];
    self.navigationItem.title = @"搞定";
    
}


#pragma mark========导航栏颜色渐变===================

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.navigationController.navigationBar setBackgroundImage:[self imageWithBgColor:[UIColor colorWithRed:0 green:1 blue:1 alpha:self.tableView.contentOffset.y / 100]] forBarMetrics:UIBarMetricsDefault];
    
}

-(UIImage *)imageWithBgColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
    
}
#pragma mark========设置tableview的头视图================

//设置头视图
-(void)setTableHeaderView{
    
    self.headV = [[HeadView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 200)];
    self.headV.backgroundColor = [UIColor greenColor];
    self.tableView.tableHeaderView = self.headV;
 
}

#pragma mark=========设置底部的两个按钮和点击事件===========

//设置底部两个按钮
-(void)addButtonAtBotttom{
    
    self.bottomButton = [[CustomTwoBtnvView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tableView.frame)-50, self.tableView.frame.size.width, 50) LeftBtnBgColor:[UIColor blackColor] RightBtnBgColor:[UIColor redColor]];
    [self.bottomButton setLeftBtnTitle:@"+关注" RightBtnTitle:@"+好友" forState:(UIControlStateNormal)];
    
    [self.bottomButton setLeftBtnTitleColor:[UIColor greenColor] RightBtnTitleColor:[UIColor greenColor]];
    
    [self.navigationController.view addSubview:self.bottomButton];
    
}

-(void)attentionBtnAction:(UIButton*)sender{
    
    NSLog(@"关注你");
    
}

-(void)addFriendBtnAction:(UIButton*)sender{
    
    NSLog(@"加好友啦");
    
}

#pragma mark=========Ta的主题/回复  button的点击事件============
//点击时刷新视图到对应的cell
-(void)clickLeftBtnAction:(UIButton*)sender{
    
    if (!flag) {
        flag = YES;
        [self.tableView reloadData];
    }
    
}
//点击时刷新视图到对应的cell
-(void)clickRightBtnAction:(UIButton*)sender{
    
    if (flag) {
        flag = NO;
    [self.tableView reloadData];
    }
}
#pragma mark==========右上角UIBarButtonItem的事件============

-(void)clickRightBarButtonItemAction:(UIBarButtonItem*)sender{
    
    NSLog(@"这里设置举报");

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark ======== Table view data source的方法=====

//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    return 5;
}

//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (flag == NO) {
        
        return self.view.frame.size.height/3.5;
        
    }else{
        
        return self.view.frame.size.height/3.8;
        
    }
   

}

//创建cell并赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (flag) {
        
         TopicTableViewCell*cell = [[TopicTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"TopCell"];
        cell.nickNameLbl.text = @"狂拽炫酷吊炸天";
        cell.timeLbl.text = @"60分钟前";
        cell.topicLbl.text = @"你仅仅因为声音就上对方";
        [cell.messageBtn setTitle:@"156回复" forState:(UIControlStateNormal)];
        [cell.userPictureBtn setBackgroundImage:[UIImage imageNamed:@"55.png"] forState:(UIControlStateNormal)];

        return cell;
        
    }else {
        
        LReplyTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"LReplyCell"];
        cell.userPictureImV.image = [UIImage imageNamed:@"55.png"];
        
        return cell;
        
    }

    
}

//点击cell事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"你点击了PersonalHomePageTableViewController中的cell");
    NSLog(@"即将跳转到的页面是:发现 ——> ReplyViewController");
    
    if ([self.ControlType isEqualToString:@"push"]) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
    ReplyViewController*replyVC = [[ReplyViewController alloc]init];
    replyVC.ControlType = @"person";
    [self.navigationController pushViewController:replyVC animated:YES];
    
    }
    
}



//设置区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    

    CustomTwoBtnvView *HeaderVInSection = [[CustomTwoBtnvView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50) LeftBtnBgColor:[UIColor cyanColor] RightBtnBgColor:[UIColor cyanColor]];
    
    //初始时设置字体为红色（与加载的cell对应）
    [HeaderVInSection.leftBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    [HeaderVInSection setLeftBtnTitle:@"Ta的主题" RightBtnTitle:@"Ta的回复" forState:(UIControlStateNormal)];
    
    //添加点击事件
    [HeaderVInSection.leftBtn addTarget:self action:@selector(clickLeftBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [HeaderVInSection.rightBtn addTarget:self action:@selector(clickRightBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
 
    //刷新视图后会走这个方法，更改button的字体颜色
    [self changeButtonColor:HeaderVInSection];
    
    return HeaderVInSection;
}

//更改button的字体颜色
-(void)changeButtonColor:(CustomTwoBtnvView*)custom{
    
    if (!flag) {

        [custom.leftBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [custom.rightBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        
    }else{
        
        [custom.leftBtn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        [custom.rightBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }
  
}

//区头的高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}

//区尾的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
